﻿// -----------------------------------------------------------------------
// <copyright file="BoundaryGenerator.cs" company="Zen Design Corp">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Zen.BlackBerry.Common
{
	using System;
	using System.Text;

	/// <summary>
	/// Implementation of the <see cref="IBoundaryGenerator"/> interface.
	/// </summary>
	public class BoundaryGenerator : IBoundaryGenerator
	{
		private Random _random;

		/// <summary>
		/// Initializes a new instance of the <see cref="BoundaryGenerator"/> class.
		/// </summary>
		public BoundaryGenerator()
		{
			_random = new Random(DateTime.UtcNow.Second);
		}

		/// <summary>
		/// Returns a new MIME boundary string.
		/// </summary>
		/// <returns>
		/// A MIME boundary string 12 characters long.
		/// </returns>
		public virtual string MakeBoundary()
		{
			StringBuilder sb = new StringBuilder();

			int maxBoundaryChars = 12;
			for (int index = 0; index < maxBoundaryChars; ++index)
			{
				int value = _random.Next(62);
				if (value < 10)
				{
					sb.Append((char)('0' + value));
				}
				else if (value < 36)
				{
					sb.Append((char)('A' + (value - 10)));
				}
				else
				{
					sb.Append((char)('a' + (value - 36)));
				}
			}

			return sb.ToString();
		}
	}
}
